package com.jiyun.racelist.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.model.*;
import com.jiyun.racelist.service.*;
import com.jiyun.vo.AnswerVo;
import com.jiyun.vo.RaceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("race")
public class RaceController {

    @Autowired
    RaceService raceService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    TeamService teamService;

    @Autowired
    TeamConsumerService teamConsumerService;

    @Autowired
    XuanZeService xuanZeService;

    @Autowired
    ShijuanService shijuanService;

    @Autowired
    AnswerService answerService;

    @RequestMapping("findPage")
    public Page<RaceVo> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody RaceVo raceVo){
        return raceService.findPage(pageNum,pageSize,raceVo);
    }

//    报名
    @RequestMapping("baoming")
    public List<RaceVo> baoming(){
        String id = (String) redisTemplate.opsForValue().get("BaoMingId");
        return raceService.baoming(Integer.parseInt(id));
    }

//    立即报名
    @RequestMapping("addTeam")
    public void addTeam(@RequestBody TeamConsumer teamConsumer){
        Team team = teamService.getById(teamConsumer.getTid());
        team.setState("已报名");
        team.setNum(team.getNum()+1);
        teamService.updateById(team);
        teamConsumer.setCid(1);
        teamConsumerService.save(teamConsumer);
    }

//    查询竞赛表
    @RequestMapping("findTeam")
    public List<Team> findTeam(){
        return teamService.list();
    }

//  跳转到开始竞赛页面
    @RequestMapping("GetJingSaiId")
    public String GetJingSaiId(@RequestParam Integer id){
        redisTemplate.opsForValue().set("GetJingSaiId",id+"");
        return "欢迎来到开始竞赛页面";
    }

//  开始竞赛
    @RequestMapping("BeginJingSai")
    public List<RaceVo>BeginJingSai(){
        String id =(String) redisTemplate.opsForValue().get("GetJingSaiId");
        return raceService.BeginJingSai(Integer.parseInt(id));
    }

//  选择题
    @RequestMapping("findPage2")
    public Page<Xuanze>findPage2(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Xuanze xuanZe){
       return xuanZeService.findPage2(pageNum,pageSize,xuanZe);
    }

//   统计选择题目数量
    @RequestMapping("findTiCount")
    public Integer findTiCount(){
        return xuanZeService.findTiCount();
    }

//  添加选择题
    @RequestMapping("addXuanZe")
    public void addXuanZe(@RequestBody Xuanze xuanze){
        Shijuan shijuan = shijuanService.getById(xuanze.getXid());
        shijuan.setState("已做");
        shijuan.setNum(shijuan.getNum()+1);
        shijuanService.updateById(shijuan);

        Answer answer = new Answer();
        answer.setName(xuanze.getValue());

        answer.setTid(xuanze.getXid());
        answerService.save(answer);

//        xuanze.setStatus("已做");
//        xuanZeService.save(xuanze);
//        redisTemplate.opsForValue().set("ValueId",xuanze.getId()+"");
    }


//  回显选择题
    @RequestMapping("updateXuanZe")
    public void updateXuanZe(@RequestBody Xuanze xuanZe){
        xuanZeService.updateById(xuanZe);
    }

//    统计已做题目数量
    @RequestMapping("findYiZuo")
    public Integer findYiZuo(){
        return shijuanService.findYiZuo();
    }

//    查询父表
    @RequestMapping("findShijuan")
    public List<Shijuan>findShijuan(){
       return shijuanService.list();
    }

// 添加之后查询答案
    @RequestMapping("findValue")
    public String findValue(){
        String id = (String) redisTemplate.opsForValue().get("ValueId");
        return shijuanService.findValue(Integer.parseInt(id));
    }

//   查看排名表
    @RequestMapping("LookPaiming")
    public List<Team>LookPaiming(){
         return teamService.LookPaiming();
    }

//   排名
    @RequestMapping("Paiming")
    public Integer Paiming(){
        String id =(String) redisTemplate.opsForValue().get("GetJingSaiId");
        return teamService.Paiming(Integer.parseInt(id));
    }

//   查看分数
   @RequestMapping("LookScore")
   public Integer LookScore(){
       String id =(String) redisTemplate.opsForValue().get("GetJingSaiId");
       return teamService.LookScore(Integer.parseInt(id));
   }

//   答案展示
    @RequestMapping("showAnswer")
    public String showAnswer(@RequestParam Integer id){
       return   answerService.showAnswer(id);
//        String count =(String) redisTemplate.opsForValue().get("show1");
//       return answerService.showAnswer(Integer.parseInt(count));
    }

//  上一题
    @RequestMapping("goBefore")
    public void goBefore(){
            Integer count = shijuanService.findYiZuo();
            Integer id=count-1;
            redisTemplate.delete(id+"");
            redisTemplate.opsForValue().set("show1",id+"");
    }

//  下一题
    @RequestMapping("goNext")
    public void goNext(){
            Integer count = shijuanService.findYiZuo();
            Integer id=count;
            redisTemplate.delete(id+"");
            redisTemplate.opsForValue().set("show1",id+"");
    }

//    查看答题情况
    @RequestMapping("findDati")
    public List<Shijuan>  findDati(){
        return shijuanService.findDati();
    }

//    正确答题个数
    @RequestMapping("findTrue")
    public Integer findTrue(){
        return shijuanService.findTrue();
    }

//    错误答题个数
    @RequestMapping("findFalse")
    public Integer findFalse(){
        return shijuanService.findFalse();
    }
}


